package org.hadoop.ha;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import java.io.OutputStream;
public class HASaveHdfs extends Configured implements Tool {
    @Override
    public int run(String[] args) throws Exception {
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        Configuration conf = getConf();
        conf.set("fs.defaultFS", "hdfs://cluster");
        conf.set("dfs.nameservices", "cluster");
        conf.set("dfs.ha.namenodes.cluster", "nn1,nn2,nn3");
        conf.set("dfs.namenode.rpc-address.cluster.nn1", "server101:8020");
        conf.set("dfs.namenode.rpc-address.cluster.nn2", "server102:8020");
        conf.set("dfs.namenode.rpc-address.cluster.nn3", "server103:8020");
        conf.set("dfs.client.failover.proxy.provider.cluster",
                "org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");
        FileSystem fs = FileSystem.get(conf);
        OutputStream out = fs.create(new Path("/test/c.txt"));
        out.write("Hello中文".getBytes());
        out.close();
        fs.close();
        return 0;
    }
    public static void main(String[] args) throws Exception {
        int code = ToolRunner.run(new HASaveHdfs(), args);
        System.exit(code);
    }
}